package com.blog.controller.admin;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.blog.entity.User;
import com.blog.service.UserService;

@Controller
@RequestMapping(value="user")
public class UserController {
	//注入UserService
	@Autowired
	private UserService userService;
	/**
	 * 跳转到登录页面
	 */
	@RequestMapping(value="/loginPage.do",method=RequestMethod.GET)
	public String loginPage(Model model) {
		return "login";
	}
	/**
	 * 确认登录
	 */
	@RequestMapping(value="/login.do",method=RequestMethod.POST)
	public String login(User user,HttpServletRequest request) {
		User u=userService.login(user.getUsername(),user.getPassword());
		request.setAttribute("u", u);
		if(u!=null) {
			return "main";
		}else {
			request.setAttribute("errorInfo", "用户名或密码错误");
			return "login";
		}
		
	}
	
	/**
	 * 查询用户信息，跳转到用户信息界面
	 */
	@RequestMapping(value="/findUserInfo.do",method=RequestMethod.GET)
	public String findUserInfo(HttpServletRequest request) {
		List<User> list = userService.findUserInfo();
		request.setAttribute("list", list);
		return "userList";
	}	
	
}
